package com.qqtop10.manager.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Givevalue;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.GivevalueService;

@RequestMapping("/manager/givevalue/")
@Controller
public class GivevalueController extends BaseController {
	@Autowired
	private GivevalueService givevalueService;
	@RequestMapping("all.do")
	@ResponseBody
	public Result all(Givevalue input){
		try {
			input.setAccount(URLDecoder.decode(input.getAccount(),"UTF-8"));
			//input.setUsername(URLDecoder.decode(input.getUsername(),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			PagingArgs pagingArgs = getPagingArgs();
			int count = givevalueService.count(input);
			List<Givevalue> list = givevalueService.all(input, pagingArgs);
			return page(count, list);
		} catch (Exception e) {
			return excep(e);
			// TODO: handle exception
		}
	}
	@RequestMapping("addGiveValue.do")
	@ResponseBody
	public Result addGiveValue(HttpServletRequest request,HttpServletResponse response){
		Result result = new Result();
		try {
			//验证合法性
			String password = request.getParameter("password");
			//System.err.println(password);
			if (!super.getLogind().getPassword()
					.equals(super.md5(password).toLowerCase())) {
				result.setCode(99);
				//result.setMsg("设置支付点数失败：管理员密码错误");
				return result;
			}
			Givevalue input = new Givevalue();
			input.setAccount(request.getParameter("account"));
			input.setMoney(Float.parseFloat(request.getParameter("money")));
			input.setOrder_sn(" ");
			input.setOrder_time(now());
			input.setPay_code("后台充值");
			input.setTrade_sn(request.getParameter("trade_sn"));
			input.setTrade_status(1);
			input.setTrade_time(now());
			input.setOrder_status(1);
			input.setAudit_person(getLogindUuid());
			input.setBz(request.getParameter("bz"));
			if (givevalueService.addGiveValue(input)!=0) {
				if (givevalueService.updataUserMoney(input)!=0) {
					result.setCode(1);
					return result;
					//return ok("操作成功");
				}
				result.setCode(2);
				return result;
				//return err("钱包入账失败");
			}
			result.setCode(3);
			return result;
			//return err("充值失败");
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(500);
			result.setMsg("设置支付点数失败：" + e.getMessage());
			return result;
			// TODO: handle exception
		}
	}
	
}
